package org.boulderfoodrescue.webapp.server.pickupreport;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Logger;

import org.boulderfoodrescue.webapp.server.location.LocationDatabase;
import org.boulderfoodrescue.webapp.server.users.UserAccountsDatabase;
import org.boulderfoodrescue.webapp.shared.Location;
import org.boulderfoodrescue.webapp.shared.PickupReport;
import org.boulderfoodrescue.webapp.shared.UserAccountDetails;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Query;

public class PickupReportDatabase {
//	private static final Logger log = Logger.getLogger(PickupReportDatabase.class.getName());
//
//	public static Long addPickupRecport(PickupReport report){
//		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
//		Entity entity = pickupReportToEntity(report);
//		Long key = datastore.put(entity).getId();
//		log.info("Added new pickup report to datastore");
//		return key;
//	}
//
//	public static Long updatePickupReport(PickupReport report) {
//		Long key = null;
//		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
//		key =  datastore.put(pickupReportToEntity(report)).getId();
//		return key;
//	}
//
//	public static List<PickupReport> getPickupReports(int maxToReturn){
//		List<PickupReport> pickupReports = new LinkedList<PickupReport>();
//		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
//		Query query = new Query("report");
//		List<Entity> dPickupReport = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(maxToReturn));
//		if(dPickupReport.size() == maxToReturn)
//			log.warning("There are more than the max number of pickups in the database");
//		for(Entity dPickup : dPickupReport){
//			PickupReport report = entityToPickupReport(dPickup);
//			pickupReports.add(report);
//		}
//		return pickupReports;
//	}
//
//	private static PickupReport entityToPickupReport(Entity dReport)
//	{
//		PickupReport report = new PickupReport();
//
//		report.setDonorKey((Long) dReport.getProperty("donorKey"));
//		Location donor = LocationDatabase.getLocation(report.getDatabaseKey(), true);
//		report.setDonor(donor);
//		report.setFoodTypeViaLong((Long) dReport.getProperty("foodType"));
//		report.setItemDiscription((String) dReport.getProperty("itemDiscription"));
//		report.setNotes((String) dReport.getProperty("notes"));
//		report.setPickupDate((Date) dReport.getProperty("pickupDate"));
//		report.setPickupKey((Long) dReport.getProperty("pickupKey"));
//		report.setPickupIndex((Long) dReport.getProperty("pickupIndex"));
//		report.setRecipientKey((Long) dReport.getProperty("recipientKey"));
//		Location recipient = LocationDatabase.getLocation(report.getRecipientKey(), false);
//		report.setRecipient(recipient);
//		report.setTransportationViaLong((Long) dReport.getProperty("transportationType"));
//		report.setVolunteerKey((Long) dReport.getProperty("volunteerKey"));
//		UserAccountDetails volunteer = UserAccountsDatabase.getUser(report.getVolunteerKey());
//		report.setVolunteer(volunteer);
//		report.setWeight((Double) dReport.getProperty("weight"));
//		
//		return report;
//	}
//
//	private static Entity pickupReportToEntity(PickupReport report)
//	{
//		Entity dPickupReport;
//		if(report.getDatabaseKey() != null && report.getDatabaseKey() != 0)
//			dPickupReport = new Entity("report", report.getDatabaseKey());
//		else
//			dPickupReport = new Entity("report");
//		
//		dPickupReport.setProperty("donorKey", report.getDonorKey());
//		dPickupReport.setProperty("foodType", report.getFoodTypeAsLong());
//		dPickupReport.setProperty("itemDiscription", report.getItemDiscription());
//		dPickupReport.setProperty("notes", report.getNotes());
//		dPickupReport.setProperty("pickupDate", report.getPickupDate());
//		dPickupReport.setProperty("pickupKey", report.getPickupKey());
//		dPickupReport.setProperty("pickupIndex", report.getPickupIndex());
//		dPickupReport.setProperty("recipientKey", report.getRecipientKey());
//		dPickupReport.setProperty("transportationType", report.getTransportationAsLong());
//		dPickupReport.setProperty("volunteerKey", report.getVolunteerKey());
//		dPickupReport.setProperty("weight", report.getWeight());
//		return dPickupReport;
//	}
}
